package com.yuanren.em.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuanren.em.entity.MemberPoints;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface MemberPointsMapper extends BaseMapper<MemberPoints> {
    
    /**
     * 分页查询积分记录（包含用户信息）
     */
    @Select("<script>" +
            "SELECT mp.*, u.username as userName, u.nickname as userNickname " +
            "FROM t_member_points mp " +
            "LEFT JOIN sys_user u ON mp.user_id = u.id " +
            "WHERE 1=1 " +
            "<if test='userId != null'>" +
            "AND mp.user_id = #{userId} " +
            "</if>" +
            "<if test='changeType != null and changeType != \"\"'>" +
            "AND mp.change_type = #{changeType} " +
            "</if>" +
            "<if test='userName != null and userName != \"\"'>" +
            "AND u.nickname LIKE CONCAT('%', #{userName}, '%') " +
            "</if>" +
            "ORDER BY mp.create_time DESC" +
            "</script>")
    IPage<MemberPoints> selectPointsPageWithUser(IPage<MemberPoints> page,
                                                @Param("userId") Long userId,
                                                @Param("changeType") String changeType,
                                                @Param("userName") String userName);
    
    /**
     * 根据用户ID获取积分记录
     */
    @Select("SELECT mp.*, u.username as userName, u.nickname as userNickname " +
            "FROM t_member_points mp " +
            "LEFT JOIN sys_user u ON mp.user_id = u.id " +
            "WHERE mp.user_id = #{userId} " +
            "ORDER BY mp.create_time DESC")
    List<MemberPoints> selectByUserId(@Param("userId") Long userId);
    
    /**
     * 获取用户当前可用积分
     */
    @Select("SELECT COALESCE(SUM(points_change), 0) " +
            "FROM t_member_points " +
            "WHERE user_id = #{userId} " +
            "AND is_expired = 0")
    Integer getUserAvailablePoints(@Param("userId") Long userId);
    
    /**
     * 获取积分统计信息
     */
    @Select("SELECT " +
            "COUNT(*) as totalRecords, " +
            "SUM(CASE WHEN points_change > 0 THEN points_change ELSE 0 END) as totalEarned, " +
            "SUM(CASE WHEN points_change < 0 THEN ABS(points_change) ELSE 0 END) as totalSpent, " +
            "COUNT(DISTINCT user_id) as activeUsers " +
            "FROM t_member_points " +
            "WHERE create_time >= DATE_SUB(NOW(), INTERVAL 30 DAY)")
    PointsStatistics getPointsStatistics();
    
    /**
     * 积分统计信息内部类
     */
    class PointsStatistics {
        private Integer totalRecords;
        private Integer totalEarned;
        private Integer totalSpent;
        private Integer activeUsers;
        
        public Integer getTotalRecords() {
            return totalRecords;
        }
        
        public void setTotalRecords(Integer totalRecords) {
            this.totalRecords = totalRecords;
        }
        
        public Integer getTotalEarned() {
            return totalEarned;
        }
        
        public void setTotalEarned(Integer totalEarned) {
            this.totalEarned = totalEarned;
        }
        
        public Integer getTotalSpent() {
            return totalSpent;
        }
        
        public void setTotalSpent(Integer totalSpent) {
            this.totalSpent = totalSpent;
        }
        
        public Integer getActiveUsers() {
            return activeUsers;
        }
        
        public void setActiveUsers(Integer activeUsers) {
            this.activeUsers = activeUsers;
        }
    }
} 